﻿using System.Linq;
using System.Reflection;
using System.Web.Management;
using System.Web.Mvc;

namespace PruebaMVC
{
    public class RequiredRequestValueAttribute : ActionMethodSelectorAttribute
    {
        private const int NumberOfNoOptionalParams = 2;

        public RequiredRequestValueAttribute(params string[] valueNames)
        {
            ValueNames = valueNames;
        }

        public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
        {
            if (controllerContext.RequestContext.RouteData.Values.Count != (ValueNames.Length + NumberOfNoOptionalParams))
                return false;

            return ValueNames.All(valueName => controllerContext.RequestContext.RouteData.Values[valueName] != null);
        }

        public string[] ValueNames { get; private set; }
    }
}